<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="80px"
      >
        <el-form-item label="任务名称" prop="taskName">
          <common-input
            v-model="queryParams.taskName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="数据库名称" prop="databaseName">
          <common-input
            v-model="queryParams.databaseName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="数据库类型" prop="dbType">
          <el-select v-model="queryParams.dbType">
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in dbTypeList"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="表名" prop="tableName">
          <common-input
            v-model="queryParams.tableName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="识别规则" prop="discerRule">
          <el-select v-model="queryParams.discerRule" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in ruleList"
              :key="item.id"
              :label="item.ruleName"
              :value="item.ruleName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="执行时间" prop="beginTime">
          <el-date-picker
            v-model="dataRange"
            type="datetimerange"
            range-separator="至"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="执行人" prop="executer">
          <common-input
            v-model="queryParams.executer"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="执行结果" prop="status">
          <el-select v-model="queryParams.status">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="成功" :value="1"></el-option>
            <el-option label="失败" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <!-- <common-dept
          ref="commonDept"
          :currentParent="currentParent"
          @getDeptId="getDeptId"
          @getDeptList="getDeptList"
          @getLoading="getLoading"
        ></common-dept> -->
      </el-form>
    </common-search>
    <common-table slot="content" v-loading="loading" :data="list">
      <el-table-column
        label="任务名称"
        align="left"
        prop="taskName"
        fixed="left"
        show-overflow-tooltip
      />
      <el-table-column
        label="数据库名称"
        align="left"
        prop="databaseName"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="数据库类型"
        align="left"
        prop="dbType"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="表名"
        align="left"
        prop="tableName"
        show-overflow-tooltip
      />
      <el-table-column
        label="列名"
        align="left"
        prop="columnName"
        show-overflow-tooltip
      />
      <el-table-column
        label="数据分级"
        align="left"
        prop="levelName"
        show-overflow-tooltip
      />
      <el-table-column
        label="数据分类"
        align="left"
        prop="dataType"
        show-overflow-tooltip
      />
      <el-table-column
        label="识别规则"
        align="left"
        prop="discerRule"
        show-overflow-tooltip
      />
      <el-table-column
        label="重要数据"
        align="left"
        prop="importantData"
        show-overflow-tooltip
      />
      <el-table-column
        label="核心数据"
        align="left"
        prop="coreData"
        show-overflow-tooltip
      />
      <el-table-column
        label="执行开始时间"
        align="left"
        prop="beginTime"
        width="170"
        show-overflow-tooltip
      />
      <el-table-column
        label="执行结束时间"
        align="left"
        prop="endTime"
        width="170"
        show-overflow-tooltip
      />
      <el-table-column
        label="任务运行时长"
        width="120"
        align="left"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            scope.row.exeDuration === null ||
            scope.row.exeDuration === undefined
              ? null
              : scope.row.exeDuration + "ms"
          }}
        </template>
      </el-table-column>

      <el-table-column
        label="执行人"
        align="left"
        prop="executer"
        show-overflow-tooltip
      />
      <el-table-column
        label="执行结果"
        align="center"
        fixed="right"
        width="100px"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">成功 </el-tag>
          <el-tag type="danger" v-else-if="scope.row.status === 2"
            >失败
          </el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="left" width="60" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)"
            >详情
          </el-button>
        </template>
      </el-table-column> -->
    </common-table>
    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情 -->
    <common-info-dialog
      title="详情"
      :visible.sync="detailsOpen"
      @cancel="cancel"
    >
      <common-info-table>
        <common-info-table-cell label="日志类型">
          {{ getLabel(form.type) }}
        </common-info-table-cell>
        <common-info-table-cell label="执行开始时间" width="50%">
          {{ form.beginTime }}
        </common-info-table-cell>
        <common-info-table-cell label="执行结束时间" width="50%">
          {{ form.endTime }}
        </common-info-table-cell>
        <common-info-table-cell label="执行人">
          {{ form.executer }}
        </common-info-table-cell>
      </common-info-table>
    </common-info-dialog>
  </common-list>
</template>

<script>
import { formatDate } from "@/utils";
import BaseList from "@/mixins/BaseList";
import { queryPageList } from "@/api/datascan/markLog/markLog";
import { queryListByTypeCode } from "@/api/datascan/scanresults/column";
import { queryList } from "@/api/datahandler/discernRule";

export default {
  name: "index",
  mixins: [BaseList],
  components: {},
  data() {
    return {
      logTypeList: [
        {
          label: "任务日志",
          value: 1,
        },
        {
          label: "识别日志",
          value: 2,
        },
      ],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dbType: null,
        databaseName: null,
        tableName: null,
        discerRule: null,
        taskName: null,
        type: null,
        beginTime: null,
        endTime: null,
        status: null,
        executer: null,
      },
      dataRange: [],
      dbTypeList: [],
      ruleList: [],
      detailsOpen: false,
      form: {},
    };
  },
  created() {
    this.getList();
    this.getDataSourceTypeList();
    this.getRuleList();
  },

  methods: {
    // 获取识别规则
    getRuleList() {
      queryList({ status: 0 }).then((res) => {
        if (res && res.code === 200) {
          this.ruleList = res.rows;
        } else {
          this.dataList = [];
        }
      });
    },
    getDataSourceTypeList() {
      queryListByTypeCode().then((res) => {
        if (res && res.code === 200) {
          this.dbTypeList = res.data.dataList.reduce((r, c) => {
            r.push({
              label: c.name,
              value: c.value,
            });
            return r;
          }, []);
        } else {
          this.dbTypeList = [];
        }
      });
    },
    /** 查询执行历史列表 */
    getList() {
      this.loading = true;
      queryPageList(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.data.dataList.reduce((r, c) => {
              r.push({
                ...c,
                beginTime: formatDate(c.beginTime),
                endTime: formatDate(c.endTime),
              });
              return r;
            }, []);
            this.total = res.data.totalCount;
          } else {
            this.list = [];
            this.total = 0;
          }
        })
        .finally(() => (this.loading = false));
    },
    /** 查询按钮操作 */
    handleQuery() {
      if (this.dataRange && this.dataRange.length) {
        this.queryParams.beginTime = this.dataRange[0];
        this.queryParams.endTime = this.dataRange[1];
      } else {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dataRange = [];
      this.queryParams = this.$options.data().queryParams;
      // this.$refs.commonDept.clearDept();
    },
    handleDetail(row) {
      this.form = row;
      this.detailsOpen = true;
    },
    getLabel(type) {
      return this.logTypeList.find((item) => item.value === type)?.label;
    },
    cancel() {
      this.detailsOpen = false;
    },
  },
};
</script>
